from bs4 import BeautifulSoup
import tools
import re
'''
解析说明:
普通站点资源一般从根域名开始,不会和路径挂钩.
'''


def HasAttrs(tag:BeautifulSoup):
    return tag.has_attr("style")
# 用来解析css内容中的静态资源
def styleContentLinks(urlObj:dict,content:str = ""):
    '''
    解析样式文件内的资源
    @urlObj 被解析过的css链接对象。
    '''
    currentUrl = urlObj.get("url")
    if tools.checkUrlVaild(currentUrl) is False:
        return []
    # css文件内容
    nr:str = content
    if content =="":
        nr = tools.getContentByurl(currentUrl)
    # 处理css文件内的静态资源文件。
    url = re.findall(r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+.(?:[mp4|jpg|png|gif|jpeg|svg|webp|jpeg|JPG|PNG|avi|ico|ttf|woff|woff2|eot])', nr)
    # 针对像阿里那样无http头的情况
    static = re.findall(r'([a-zA-z.*-_]+\.(mp4|jpg|png|gif|jpeg|svg|webp|jpeg|JPG|PNG|avi|ico|ttf|woff|woff2|eot)){1,}',nr,re.S)
    staticUrls = []
    for el in url:
        temurl = tools.checkPathUrl(el[0],currentUrl,isDelPointPath=True)
        if temurl.get("suffix") != "":
            temurl["oldvalue"] = el[0]
            staticUrls.append(temurl)
        
    for el in static:
        rulst = tools.checkPathUrl(el[0],currentUrl,isDelPointPath=True)
        rulst["oldvalue"] = el[0]
        staticUrls.append(rulst)

    return staticUrls
# 页面上的cssLinks解析。
def pageBodyLinksCss(bs:BeautifulSoup,url:str="")->list:
    '''
    通过html链接url地址解析css文件链接，并解析对应的css文件内容中的静态资源
    @bs BeautifulSoup对象的网页内容
    @url 目标网址地址
    '''
    
    csslist = bs.find_all("link",attrs={"rel":"stylesheet"})
    # paths = tools.urlParse(url)
    # css路径文件. 
    csslisturl:list[dict] = []
    for el in csslist:
        rulst = tools.checkPathUrl(el.get("href"),url)
        rulst["oldvalue"] = el.get("src")
        csslisturl.append(rulst)
    # 解析css中的资源路径。
    statickUrl = []
    for el in csslisturl:
        temurl = styleContentLinks(el)
        if len(temurl)>0:
            statickUrl+=temurl
    
    return csslisturl+statickUrl
# 解析页面显性的script的js链接文件
def pageBodyLinksJs(bs:BeautifulSoup,url:str="")->list:
    '''
    页面上的Js链接解析。
    @bs BeautifulSoup对象的网页内容
    @url 目标网址地址
    '''
    
    jslist = bs.find_all("script")
    jslistFilter = [];
    for el in jslist:
        if el.has_attr("src"):
            jslistFilter.append(el)
    csslisturl:list[dict] = []
    for el in jslistFilter:
        rulst = tools.checkPathUrl(el.get("src"),url)
        rulst["oldvalue"] = el.get("src")
        csslisturl.append(rulst)

    return csslisturl
# 解析页面显性的静态资源
def pageBodyLinksImg(bs:BeautifulSoup,url:str="")->list:
    '''
    页面上的静态文件路径解析。
    @bs BeautifulSoup对象的网页内容
    @url 目标网址地址
    '''
    
    imglist = bs.find_all("img")
    videolist = bs.find_all("video")
    videosourcelist = bs.find_all("source")
    audiolist = bs.find_all("audio")
    jslistFilter = [];
    for el in imglist+audiolist+videosourcelist:
        if el.has_attr("src"):
            jslistFilter.append(el)
    for el in videolist:
        if el.has_attr("poster"):
            jslistFilter.append({"src":el.get("poster")})
    jslistFilterReal = []
    for el in jslistFilter:
        if el.get("src")[0:4]!="data":
            jslistFilterReal.append(el)
    
    # 视频的话，需要解析封面资源图路径。
    csslisturl:list[dict] = []
    for el in jslistFilterReal:
        rulst = tools.checkPathUrl(el.get("src"),url)
        rulst["oldvalue"] = el.get("src")
        csslisturl.append(rulst)

    return csslisturl
# 解析页面中的style标签内容中的静态资源.
def pageBOdyStyleContent(bs:BeautifulSoup,url:str="")->list:
    '''
    解析页面中的style标签内容中的静态资源.
    @bs BeautifulSoup对象的网页内容
    @url 目标网址地址
    '''
    csslist = bs.find_all("style")
    staticlist = []
    for el in csslist:
        cssContent = el.get_text()
        temlist =  styleContentLinks(tools.urlParse(url),cssContent)
        staticlist+=temlist
    
    # 这里还要解析页面上标签的静态资源.
    divstylelist = bs.find_all(HasAttrs)

    for el in divstylelist:
        cssContent = el.get("style")
        temlist =  styleContentLinks(tools.urlParse(url),cssContent)
        staticlist+=temlist
    


    return staticlist
    
